package com.huawei.ec.ttt.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.huawei.ec.ttt.model.User;
import com.huawei.ec.ttt.service.UserServiceImpl;


public class UserController {

	@Autowired
	private UserServiceImpl userServiceT;

	private List<User> userList;

	@RequestMapping(value = "/getAll", method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request) {
		userList = userServiceT.getUserList();
		ModelAndView view = new ModelAndView();
		view.setViewName("result");
		view.addObject("userList", userList);// 添加数据模型
		return view;
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView addUser(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
		User user = new User();
		user.setName(username);
		user.setPassword(password);
		userServiceT.AddUser(user);
		ModelAndView view = new ModelAndView();
		view.setViewName("register");
		return view;
	}
	
	@RequestMapping(value = "/register1", method = RequestMethod.POST)
	public ModelAndView addUser1(@ModelAttribute User user) {
		userServiceT.AddUser(user);
		ModelAndView view = new ModelAndView();
		view.setViewName("register");
		return view;
	}
	
}
